package com.itouch.bracelet.activity;

import io.rong.imkit.RongIM;
import io.rong.imkit.RongIM.OperationCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.RongIMClient.Group;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.adapter.RGroupAdapter;
import com.itouch.bracelet.bean.LoginUser;
import com.itouch.bracelet.bean.RGroup;
import com.itouch.bracelet.cache.UserCache;

public class GroupActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener, RongIM.GetGroupInfoProvider {

	private ListView group_listview;
	private static final String LOG_TAG = "GroupActivity";
	private LoginUser user;
	private RGroupAdapter adapter;
	private List<RGroup> list;
	private ArrayList<Group> gList;

	private TextView back;
	

	@Override
	protected int setContentViewById() {
		// TODO Auto-generated method stub
		return R.layout.activity_group;
	}

	@Override
	protected void initViews() {
		user = UserCache.getOauth(this);
		list = new ArrayList<RGroup>();
		if(user!=null){
			list.addAll(user.getRongyungroup());
		}
		adapter = new RGroupAdapter(this, list);
		group_listview = (ListView) findViewById(R.id.group_listview);
		group_listview.setAdapter(adapter);
		group_listview.setOnItemClickListener(this);
		RongIM.getInstance().setGetGroupInfoProvider(GroupActivity.this);
		
	}

	@Override
	protected void initData() {
		
	}

	
	

	private void initGroupInfo(List<RGroup> list) {

		if(list==null ||list.size()<1){
			return;
		}
		gList = new ArrayList<RongIMClient.Group>();
		for (int i = 0; i < list.size(); i++) {
			RGroup a = list.get(i);
			RongIMClient.Group RGroup = new RongIMClient.Group(a.getId()
					.toString(), a.getGroupName().toString(), "");
			gList.add(RGroup);
		}

		
		RongIM.getInstance().syncGroup(gList, new RongIM.OperationCallback() {

			@Override
			public void onSuccess() {
			}

			@Override
			public void onError(ErrorCode errorCode) {
			}
		});

	}

	public void joinGroup() {
		for (int i = 0; i < gList.size(); i++) {
			Group a = gList.get(i);

			RongIM.getInstance().joinGroup(a.getId(), a.getName(),
					new OperationCallback() {

						@Override
						public void onSuccess() {
						}

						@Override
						public void onError(ErrorCode arg0) {

						}
					});
		}

	}

	

	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		RGroup a = list.get(arg2);
		RongIM.getInstance().startGroupChat(this, a.getId(),
				a.getGroupName().toString());
	}

	@Override
	public Group getGroupInfo(String arg0) {
		if (gList != null) {
			for (int i = 0; i < gList.size(); i++) {
				Group a = gList.get(i);
				if (a.getId().equals(arg0)) {
					return a;
				}
			}
		}
		return null;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	
}
